package com.hp.attendance.serlvet.oneself;

import com.hp.attendance.entity.Employee;
import com.hp.attendance.entity.Leave;
import com.hp.attendance.entity.PageInfo;
import com.hp.attendance.entity.dto.MenuDto;
import com.hp.attendance.serlvet.leave.LeaveListServlet;
import com.hp.attendance.service.EmployeeService;
import com.hp.attendance.service.LeaveService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/onseifLeavelist")
public class OneselfLeavelistServlet extends HttpServlet {
    private LeaveService leaveService=new LeaveService();
    private EmployeeService employeeService=new EmployeeService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Employee emp = (Employee) req.getSession().getAttribute("emp");
            Employee employee = employeeService.queryEmployee(emp.getId());
            Integer employee_number = employee.getEmployee_number();
            String pageNo = req.getParameter("pageNo");
            if(pageNo==null ||"".equals(pageNo.trim())){
                pageNo="1";
            }
            PageInfo<Leave> page = leaveService.getPage(employee_number,Integer.parseInt(pageNo), 10);
            req.setAttribute("page",page);
            req.setAttribute("employee",employee);
            req.getRequestDispatcher("/pages/oneself_leave.jsp").forward(req,resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
